package com.murfrosoft.views;

import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public abstract class AnimationViewAbstractBase extends SurfaceView implements Runnable {

	private Thread actionThread=null;
	private SurfaceHolder holder=null;
	private volatile Boolean running=false;
	
	public AnimationViewAbstractBase(Context context) {
		super(context);
		holder = getHolder();
	}
	
	//call this when activity is resumed
	public void go(){
		
		actionThread = new Thread(this);
		actionThread.start();
		running = true;
	}

	//call this when activity is paused
	public void stop(){
	      running=false;
		try{
    	   actionThread.join();
       }catch(InterruptedException ex){
    	   
       }
 
	}
	
	@Override
	public void run(){
		while(running){
			if(!holder.getSurface().isValid()){continue;}
			Canvas canvas = holder.lockCanvas();
			
			updatePhysics();
			draw(canvas);
			
			holder.unlockCanvasAndPost(canvas);
			
		}
	}


public abstract void updatePhysics();
public abstract void draw(Canvas canvas);

}
	